package com.enjoyor.soa.traffic.server.tsms.helper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.enjoyor.soa.traffic.core.tsms.domain.RltRouteDevice;
import com.enjoyor.soa.traffic.core.tsms.domain.RouteInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.RoutePoint;
import com.enjoyor.soa.traffic.core.tsms.domain.SchemeInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.TaskDetailInfo;
import com.enjoyor.soa.traffic.core.tsms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.core.tsms.service.IRouteInfoService;
import com.enjoyor.soa.traffic.util.helper.GeoToolsHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
/**
 * 处理线路数据
 * @author fzj
 * @date 2019-05-31
 */
public class RouteHandleHelper {
	private static final Logger logger = Logger.getLogger(RouteHandleHelper.class);
	/**
	 * 将线路信息按每秒时间进行分解
	 * @param taskRouteMap 单条线路信息
	 * 		@param taskId 任务ID
	 * 		@param taskName 任务名称
	 * 		@param routeInfo 线路信息
	 * 		@param beginTime 开始执行时间
	 * 		@param endTime 结束执行时间
	 * 		@param startLength 距线路起点距离
	 * @param routeSplitTime 线路拆分时间间隔(ms)
	 * @param isExecute 任务是否正在执行
	 * @return timeMap
	 */
	@SuppressWarnings({ "static-access", "rawtypes"})
	public static Map<String, Map<String, Map>> routeInfoByTimeOne(Map<String, Map<String, Map>> timeMap, 
			Map<String, Object> taskRouteMap, int routeSplitTime, boolean isExecute) {
		//long start = new Date().getTime();
		GeoToolsHelper geoToolsHelper = new GeoToolsHelper(); // geo计算方法
		try {
			long time1 = new Date().getTime();
		    String taskId = (String)taskRouteMap.get("taskId");//任务ID
		    String taskName = (String)taskRouteMap.get("taskName");//任务名称
		    RouteInfo routeInfo = (RouteInfo)taskRouteMap.get("routeInfo");//线路信息
			String beginTime = (String)taskRouteMap.get("beginTime");//开始执行时间
			String endTime = (String)taskRouteMap.get("endTime");//结束执行时间
		    double startLength = (double)taskRouteMap.get("startLength");//距线路起点距离
		    List<RoutePoint> routePointList = routeInfo.getRoutePointList(); // 线路途径点集合
			List<RltRouteDevice> rltRouteDevicelist = routeInfo.getRltRouteDeviceList();// 线路关联设备集合
			List<RltRouteDevice> videoListAll = new ArrayList<RltRouteDevice>();//所有关联的视频点位
			List<RltRouteDevice> signalListAll = new ArrayList<RltRouteDevice>();//所有关联的信号机
			for (int i = 0; i < rltRouteDevicelist.size(); i++) {
				if (rltRouteDevicelist.get(i).getDeviceTypeId().equals("RLTDEVICETYPE01")) {
					videoListAll.add(rltRouteDevicelist.get(i));
				}else if (rltRouteDevicelist.get(i).getDeviceTypeId().equals("RLTDEVICETYPE02")) {
					signalListAll.add(rltRouteDevicelist.get(i));
				}
			}
			//关联设备信息根据距离排序
			if (videoListAll != null && videoListAll.size() > 0) {
				videoListAll = rltRouteDeviceSort(videoListAll);
			}
			if (signalListAll != null && signalListAll.size() > 0) {
				signalListAll = rltRouteDeviceSort(signalListAll);
			}
			long timeKey = TimeHelper.StringToDateTime(beginTime, "yyyy-MM-dd HH:mm:ss");//开始时间转化为时间戳
			RoutePoint endRoutePoint = routePointList.get(routePointList.size()-1);//终点信息
			int routeLength = routeInfo.getRouteLength().intValue();//线路总长度
			if(startLength < 0 || startLength > routeLength) {
				return timeMap;
			}
			int iPoint = -1;//途径点序号
			int iVideo = -1;//关联视频点位序号
			int iSignal = -1;//关联信号机序号
			boolean videoFlag = true;//视频点位是否推送标记，默认第一次为true
			boolean signalFlag = true;//信号机是否推送标记，默认第一次为true
			//获取上个途径点序号
			iPoint = getRoutePointNum(routePointList, startLength);
			if(iPoint < 0) {
				return timeMap;
			}
			//获取上个视频点位序号
			iVideo = getRouteDeviceNum(videoListAll, startLength);
			//获取上个信号机序号
			iSignal = getRouteDeviceNum(signalListAll, startLength);
			
			double distanceStartLength = startLength;//距起点距离
			double speed = routePointList.get(iPoint+1).getSpeed();// 速度,单位为千米/小时
			//当前时刻视频点位、信号机集合
			Map<String, RltRouteDevice> videoMap = getRealDeviceMap(videoListAll, iVideo, 1);
			Map<String, RltRouteDevice> signalMap = getRealDeviceMap(signalListAll, iSignal, 4);
			long time2 = new Date().getTime();
			while (speed >= 0 && distanceStartLength <= routeLength){
				Map<String, Map> taskMap = new HashMap<String, Map>();
				Map<String, Object> objectMap = new HashMap<String, Object>();
				
				if(iVideo >= -1 && iVideo < videoListAll.size() -1) {
					//下一个视频点位的距离
					int nextVideoLength = videoListAll.get(iVideo+1).getDeviceLength().intValue();
					if(distanceStartLength >= nextVideoLength) {
						videoFlag = true;
						iVideo++;
						videoMap.clear();
						videoMap = getRealDeviceMap(videoListAll, iVideo, 1);
					}
				}
				Map<String, RltRouteDevice> videoMapTemp = new HashMap<String, RltRouteDevice>();
				videoMapTemp.putAll(videoMap);
				if(iSignal >= -1 && iSignal < signalListAll.size() -1) {
					//下一个信号机的距离
					int nextSignalLength = signalListAll.get(iSignal+1).getDeviceLength().intValue();
					if(distanceStartLength >= nextSignalLength) {
						signalFlag = true;
						iSignal++;
						signalMap.clear();
						signalMap = getRealDeviceMap(signalListAll, iSignal, 4);
					}
				}
				Map<String, RltRouteDevice> signalMapTemp = new HashMap<String, RltRouteDevice>();
				signalMapTemp.putAll(signalMap);
				//下一个途径点相关信息
				String nextLeaveTime = beginTime.split(" ")[0] + " " +routePointList.get(iPoint+1).getPlanTime() + ":00";
				int nextPointLength = routePointList.get(iPoint+1).getPointLength().intValue();
				double nextParkTime = routePointList.get(iPoint+1).getParkTime();
				long nextParkTimeL = (long)nextParkTime*60*1000;//停留时间转化为毫秒
				long leaveTimeL = TimeHelper.StringToDateTime(nextLeaveTime, "yyyy-MM-dd HH:mm:ss");//下个途径点离开时间戳
				long arriveTimeL = leaveTimeL - nextParkTimeL;//下个途径点到达时间戳
				//距离或者时间超过下一个途径点，则进入下一个途径点
				double xxx = 0;
				double yyy = 0;
				boolean isOverTime = timeKey >= arriveTimeL;//当前时刻是否超过到达途径点时间
				//如果是实际执行线路时：不考虑途径点的到达时间，isOverTime默认为false
				if(isExecute) {
					isOverTime = false;
				}
				if(distanceStartLength >= nextPointLength || isOverTime) {
					iPoint++;//下一个途径点
					//超过途径点，则默认途径点到起点距离
					double pointX = routePointList.get(iPoint).getPointX();
					double pointY = routePointList.get(iPoint).getPointY();
					distanceStartLength = routePointList.get(iPoint).getPointLength().intValue();
					int distanceNextLength = 0;
					//判断是否终点,不是终点才计算
					if(iPoint != routePointList.size()-1) {
						distanceNextLength = routePointList.get(iPoint+1).getSectionLength().intValue();
						speed = routePointList.get(iPoint+1).getSpeed();
					} else {
						speed = routePointList.get(iPoint).getSpeed();
					}
					objectMap.put("iPoint", iPoint);
					objectMap.put("x", pointX);
					objectMap.put("y", pointY);
					objectMap.put("distanceStartLength", distanceStartLength);//距起点距离
					objectMap.put("distanceNextLength", distanceNextLength);//距下个途径点距离
					objectMap.put("speed", speed);
					objectMap.put("videoFlag", videoFlag);
					objectMap.put("videoMap", videoMapTemp);
					objectMap.put("signalFlag", signalFlag);
					objectMap.put("signalMap", signalMapTemp);
					objectMap.put("taskName", taskName);
					//如果是实际执行线路时：不考虑途径点的到达时间，某途径点离开时间为当前时间时刻加上停留时间
					if(isExecute) {
						isOverTime = false;
						leaveTimeL = timeKey + nextParkTimeL;
					}
					//时间戳小于离开时间时，该时间段内都停留在途径点位置
					for(long time=timeKey; timeKey<leaveTimeL; timeKey+=routeSplitTime) {
						//取出timeMap中原有的值，如果存在则更新，不存在则新增
						Map<String, Map> taskMapTemp = timeMap.get(String.valueOf(timeKey));
						if(taskMapTemp == null) {
							taskMap.put(taskId, objectMap);
							timeMap.put(String.valueOf(timeKey), taskMap);
						}else {
							taskMapTemp.put(taskId, objectMap);
							timeMap.put(String.valueOf(timeKey), taskMapTemp);
						}
						/*String dateStr = TimeHelper.dateToLongString(new Date(timeKey));
						String message =  dateStr+ "---------执行任务：" + taskId + "===下一个时间："+pointX+"--距起点："+pointY;
						logger.info(message);*/
					}
				}else {
					double[] geo = geoToolsHelper.LinearEquationXY(routeInfo.getRouteGeo(), distanceStartLength); // 取得经纬度
					int distanceNextLength = routePointList.get(iPoint+1).getPointLength().intValue() - (int)distanceStartLength;
					if(geo == null) {
						//默认终点坐标
						double endX = endRoutePoint.getPointX();
						double endY = endRoutePoint.getPointY();
						objectMap.put("x", endX);
						objectMap.put("y", endY);
						xxx = endX;
						yyy = endY;
					}else {
						objectMap.put("x", geo[0]);
						objectMap.put("y", geo[1]);
						xxx = geo[0];
						yyy = geo[1];
					}
					objectMap.put("iPoint", iPoint);
					objectMap.put("distanceStartLength", distanceStartLength);//距起点距离
					objectMap.put("distanceNextLength", distanceNextLength);//距下个途径点距离
					objectMap.put("speed", speed);
					objectMap.put("videoFlag", videoFlag);
					objectMap.put("videoMap", videoMapTemp);
					objectMap.put("signalFlag", signalFlag);
					objectMap.put("signalMap", signalMapTemp);
					objectMap.put("taskName", taskName);
				}
				//取出timeMap中原有的值，如果存在则更新，不存在则新增
				Map<String, Map> taskMapTemp = timeMap.get(String.valueOf(timeKey));
				if(taskMapTemp == null) {
					taskMap.put(taskId, objectMap);
					timeMap.put(String.valueOf(timeKey), taskMap);
				}else {
					taskMapTemp.put(taskId, objectMap);
					timeMap.put(String.valueOf(timeKey), taskMapTemp);
				}
				//更新下一间隔的时间、距离
				timeKey += routeSplitTime;//下一间隔时间
				double m = (speed * 1000 / 3600)*routeSplitTime/1000;//每间隔距离
				distanceStartLength += m;//下一间隔距离
				//关联设备的是否推送标记重置为false
				videoFlag = false;
				signalFlag = false;
				/*String dateStr = TimeHelper.dateToLongString(new Date(timeKey));
				String message =  dateStr+ "---------执行任务：" + taskId + "===下一个时间："+xxx+"--距起点："+yyy;
				logger.info(message);*/ 
			}
			long time3 = new Date().getTime();
			//System.out.println(taskName+" 线路拆分循环时间"+ (time3 - time2) +"ms--获取拆分基础数据时间"+(time2 - time1)+"ms");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//long end = new Date().getTime();
		//System.out.println("线路拆分结束时间"+ (end - start) +"ms");
		return timeMap;
	}
	
	/**
	 * 根据方案获取线路列表信息
	 * @param schemeList 方案列表集合（包含执行任务）
	 * @return taskRouteMap
	 * @param minBeginLong 任务最小开始时间
	 * @param maxEndLong 任务最大结束时间
	 * @param taskRouteList 线路列表集合
	 */
	public static Map<String, Object> getRouteListByScheme(List<SchemeInfo> schemeList,IRouteInfoService iRouteInfoService){
		Map<String, RouteInfo> routeInfoMap = new HashMap<String, RouteInfo>();
		Map<String, Object> taskRouteMap = getRouteListBySchemeNew(schemeList, routeInfoMap, iRouteInfoService);
		return taskRouteMap;
	}
	
	public static Map<String, Object> getRouteListBySchemeNew(List<SchemeInfo> schemeList, 
			Map<String, RouteInfo> routeInfoMap,IRouteInfoService iRouteInfoService) {
		Map<String, Object> taskRouteMap = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> taskRouteList = new ArrayList<Map<String, Object>>();
			long minBeginLong = 9999999999999l; //最小开始时间戳
			long maxEndLong = 0;//最大结束时间戳
			for (int j = 0; j < schemeList.size(); j++) {
				List<TaskDetailInfo> taskDetaillist = schemeList.get(j).getTaskDetailInfoList(); // 每个方案多个任务
				if (taskDetaillist.size() > 0) {
					// 合并所有线路
					for (int k = 0; k < taskDetaillist.size(); k++) {
						String taskId = taskDetaillist.get(k).getTaskId(); //任务ID作为key
						String taskName = taskDetaillist.get(k).getTaskName();
						String executeDate = taskDetaillist.get(k).getExecuteDate(); //执行日期
						double startLength = 0.0; //距起点长度，默认为0
						//List<RouteInfo> routeInfoListK = taskDetaillist.get(k).getRouteInfoList();
						List<RouteInfo> routeInfoListK = iRouteInfoService.getRouteInfoList(null, null, taskId, null, 
								null, EnumDicPublic.ROUTE_EXECUTE.getKey());
						if(routeInfoListK.size() > 0 ) {
							String routId = routeInfoListK.get(0).getRouteId();
							//先判断是否存在修改的线路信息，有修改的信息优先使用，没有的情况再使用查询的线路信息
							RouteInfo routeInfo = routeInfoMap.get(routId);
							if(routeInfo == null) {
								routeInfo = routeInfoListK.get(0);
							}
							List<RoutePoint> routePointList = routeInfo.getRoutePointList();
							if(routePointList.size() > 0) {
								//执行开始时间
								String beginTime = executeDate + " " + routePointList.get(0).getPlanTime() + ":00";
								//执行结束时间
								String endTime = executeDate + " " + routePointList.get(routePointList.size()-1).getPlanTime() + ":00";
								// 标准时间
								long beginLong = TimeHelper.StringToDateTime(beginTime, "yyyy-MM-dd HH:mm:ss");//开始时间转化为时间戳
								long endLong = TimeHelper.StringToDateTime(endTime, "yyyy-MM-dd HH:mm:ss");//开始时间转化为时间戳
								//String nextLeaveTime = beginTime.split(" ")[0] + " " +routePointList.get(iPoint+1).getPlanTime() + ":00";
								Map<String, Object> routeMap = new HashMap<String, Object>();
								routeMap.put("taskId", taskId);
								routeMap.put("taskName", taskName);
								routeMap.put("routeInfo", routeInfo);
								routeMap.put("beginTime", beginTime);
								routeMap.put("endTime", endTime);
								routeMap.put("startLength", startLength);
								taskRouteList.add(routeMap);
								minBeginLong = Math.min(beginLong, minBeginLong);//取最小值
								maxEndLong = Math.max(endLong, maxEndLong);//取最大值
							}
						}
					}
				}
			}
			if(taskRouteList != null && taskRouteList.size() > 0) {
				taskRouteMap.put("minBeginLong", minBeginLong);
				taskRouteMap.put("maxEndLong", maxEndLong);
				taskRouteMap.put("taskRouteList", taskRouteList);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return taskRouteMap;
	}
	
	/**
	 * 关联设备信息根据距离排序
	 * @param rltRouteDeviceList 所有关联设备信息集合
	 * @return
	 */
	public static List<RltRouteDevice> rltRouteDeviceSort(List<RltRouteDevice> rltRouteDeviceList) {
		Collections.sort(rltRouteDeviceList, new Comparator<RltRouteDevice>() {
			/*
			 * int compare(Person p1, Person p2) 返回一个基本类型的整型， 返回负数表示：p1
			 * 小于p2， 返回0 表示：p1和p2相等， 返回正数表示：p1大于p2
			 */
			@Override
			public int compare(RltRouteDevice p1, RltRouteDevice p2) {
				if (p1.getDeviceLength() > p2.getDeviceLength()) {
					return 1;
				}
				if (p1.getDeviceLength() == p2.getDeviceLength()) {
					return 0;
				}
				return -1;
			}

		});
		return rltRouteDeviceList;
	}
	
	/**
	 * 根据距起点距离获取上一个途径点序号
	 * @param routePointList 途径点信息集合
	 * @param startLength 距起点距离
	 * @return
	 */
	public static int getRoutePointNum(List<RoutePoint> routePointList, double startLength) {
		int iPoint = -1;
		for(int p = 0; p < routePointList.size()-1; p++) {
			if(startLength >= routePointList.get(p).getPointLength().intValue() 
					&& startLength < routePointList.get(p+1).getPointLength().intValue()) {
				iPoint = p;
				break;
			}
		}
		return iPoint;
	}
	/**
	 * 根据距起点距离获取关联设备序号
	 * @param deviceListAll 关联设备信息集合
	 * @param startLength 距起点距离
	 * @return
	 */
	public static int getRouteDeviceNum(List<RltRouteDevice> deviceListAll, double startLength) {
		int iDevice = -1;
		for(int p = 0; p < deviceListAll.size()-1; p++) {
			if(startLength >= deviceListAll.get(p).getDeviceLength().intValue() 
					&& startLength < deviceListAll.get(p+1).getDeviceLength().intValue()) {
				iDevice = p;
				break;
			}
		}
		return iDevice;
	}
	/**
	 * 获取当前时刻的设备点位
	 * @param deviceListAll 关联设备信息集合
	 * @param iDevice 当前设备序号
	 * @param deviceNum 当前位置前后个数（视频为1、信号机为4）
	 * @return
	 */
	//获取当前时刻的视频点位(当前位置前后各一个)
	public static Map<String, RltRouteDevice> getRealDeviceMap(List<RltRouteDevice> deviceListAll, int iDevice, int deviceNum) {
		Map<String, RltRouteDevice> videoMap = new HashMap<String, RltRouteDevice>();
		for (int i = 1 - deviceNum; i < deviceNum + 1; i++) {
			int k = iDevice + i;
			if (k >= 0 && k < deviceListAll.size()) {
				RltRouteDevice rltRouteDevice = ReflectHelper.beanToBean(deviceListAll.get(k), RltRouteDevice.class);
				//videoMap.put("deviceXh", i);
				rltRouteDevice.setId(String.valueOf(i));
				videoMap.put(deviceListAll.get(k).getDeviceId(), rltRouteDevice);
			}
		}
		return videoMap;
	}

	/**
	 * 将线路信息按每秒时间进行分解（基本不用）
	 * @param taskRouteList 所有线路信息集合
	 * 		@param taskId 任务ID
	 * 		@param taskName 任务名称
	 * 		@param routeInfo 线路信息
	 * 		@param beginTime 开始执行时间
	 * 		@param endTime 结束执行时间
	 * 		@param startLength 距线路起点距离
	 * @param routeSplitTime 线路拆分时间间隔(ms)
	 * @param isExecute 任务是否正在执行
	 * @return
	 */
	@SuppressWarnings({ "static-access", "rawtypes"})
	public static Map<String, Map<String, Map>> routeInfoByTime(List<Map<String, Object>> taskRouteList, int routeSplitTime, boolean isExecute) {
		//long start = new Date().getTime();
		GeoToolsHelper geoToolsHelper = new GeoToolsHelper(); // geo计算方法
		Map<String, Map<String, Map>> timeMap = new HashMap<String, Map<String, Map>>();
		try {
			for(Map<String, Object> taskRouteMap : taskRouteList){
				long time1 = new Date().getTime();
			    String taskId = (String)taskRouteMap.get("taskId");//任务ID
			    String taskName = (String)taskRouteMap.get("taskName");//任务名称
			    RouteInfo routeInfo = (RouteInfo)taskRouteMap.get("routeInfo");//线路信息
				String beginTime = (String)taskRouteMap.get("beginTime");//开始执行时间
				String endTime = (String)taskRouteMap.get("endTime");//结束执行时间
			    double startLength = (double)taskRouteMap.get("startLength");//距线路起点距离
			    List<RoutePoint> routePointList = routeInfo.getRoutePointList(); // 线路途径点集合
				List<RltRouteDevice> rltRouteDevicelist = routeInfo.getRltRouteDeviceList();// 线路关联设备集合
				List<RltRouteDevice> videoListAll = new ArrayList<RltRouteDevice>();//所有关联的视频点位
				List<RltRouteDevice> signalListAll = new ArrayList<RltRouteDevice>();//所有关联的信号机
				for (int i = 0; i < rltRouteDevicelist.size(); i++) {
					if (rltRouteDevicelist.get(i).getDeviceTypeId().equals("RLTDEVICETYPE01")) {
						videoListAll.add(rltRouteDevicelist.get(i));
					}else if (rltRouteDevicelist.get(i).getDeviceTypeId().equals("RLTDEVICETYPE02")) {
						signalListAll.add(rltRouteDevicelist.get(i));
					}
				}
				//关联设备信息根据距离排序
				if (videoListAll != null && videoListAll.size() > 0) {
					videoListAll = rltRouteDeviceSort(videoListAll);
				}
				if (signalListAll != null && signalListAll.size() > 0) {
					signalListAll = rltRouteDeviceSort(signalListAll);
				}
				long timeKey = TimeHelper.StringToDateTime(beginTime, "yyyy-MM-dd HH:mm:ss");//开始时间转化为时间戳
				RoutePoint endRoutePoint = routePointList.get(routePointList.size()-1);//终点信息
				int routeLength = routeInfo.getRouteLength().intValue();//线路总长度
				if(startLength < 0 || startLength > routeLength) {
					continue;
				}
				int iPoint = -1;//途径点序号
				int iVideo = -1;//关联视频点位序号
				int iSignal = -1;//关联信号机序号
				boolean videoFlag = true;//视频点位是否推送标记，默认第一次为true
				boolean signalFlag = true;//信号机是否推送标记，默认第一次为true
				//获取上个途径点序号
				iPoint = getRoutePointNum(routePointList, startLength);
				if(iPoint < 0) {
					continue;
				}
				//获取上个视频点位序号
				iVideo = getRouteDeviceNum(videoListAll, startLength);
				//获取上个信号机序号
				iSignal = getRouteDeviceNum(signalListAll, startLength);
				
				double distanceStartLength = startLength;//距起点距离
				double speed = routePointList.get(iPoint+1).getSpeed();// 速度,单位为千米/小时
				//当前时刻视频点位、信号机集合
				Map<String, RltRouteDevice> videoMap = getRealDeviceMap(videoListAll, iVideo, 1);
				Map<String, RltRouteDevice> signalMap = getRealDeviceMap(signalListAll, iSignal, 4);
				long time2 = new Date().getTime();
				while (speed >= 0 && distanceStartLength <= routeLength){
					Map<String, Map> taskMap = new HashMap<String, Map>();
					Map<String, Object> objectMap = new HashMap<String, Object>();
					
					if(iVideo >= -1 && iVideo < videoListAll.size() -1) {
						//下一个视频点位的距离
						int nextVideoLength = videoListAll.get(iVideo+1).getDeviceLength().intValue();
						if(distanceStartLength >= nextVideoLength) {
							videoFlag = true;
							iVideo++;
							videoMap.clear();
							videoMap = getRealDeviceMap(videoListAll, iVideo, 1);
						}
					}
					Map<String, RltRouteDevice> videoMapTemp = new HashMap<String, RltRouteDevice>();
					videoMapTemp.putAll(videoMap);
					if(iSignal >= -1 && iSignal < signalListAll.size() -1) {
						//下一个信号机的距离
						int nextSignalLength = signalListAll.get(iSignal+1).getDeviceLength().intValue();
						if(distanceStartLength >= nextSignalLength) {
							signalFlag = true;
							iSignal++;
							signalMap.clear();
							signalMap = getRealDeviceMap(signalListAll, iSignal, 4);
						}
					}
					Map<String, RltRouteDevice> signalMapTemp = new HashMap<String, RltRouteDevice>();
					signalMapTemp.putAll(signalMap);
					//下一个途径点相关信息
					String nextLeaveTime = beginTime.split(" ")[0] + " " +routePointList.get(iPoint+1).getPlanTime() + ":00";
					int nextPointLength = routePointList.get(iPoint+1).getPointLength().intValue();
					double nextParkTime = routePointList.get(iPoint+1).getParkTime();
					long nextParkTimeL = (long)nextParkTime*60*1000;//停留时间转化为毫秒
					long leaveTimeL = TimeHelper.StringToDateTime(nextLeaveTime, "yyyy-MM-dd HH:mm:ss");//下个途径点离开时间戳
					long arriveTimeL = leaveTimeL - nextParkTimeL;//下个途径点到达时间戳
					//距离或者时间超过下一个途径点，则进入下一个途径点
					double xxx = 0;
					double yyy = 0;
					boolean isOverTime = timeKey >= arriveTimeL;//当前时刻是否超过到达途径点时间
					//如果是实际执行线路时：不考虑途径点的到达时间，isOverTime默认为false
					if(isExecute) {
						isOverTime = false;
					}
					if(distanceStartLength >= nextPointLength || isOverTime) {
						iPoint++;//下一个途径点
						//超过途径点，则默认途径点到起点距离
						double pointX = routePointList.get(iPoint).getPointX();
						double pointY = routePointList.get(iPoint).getPointY();
						distanceStartLength = routePointList.get(iPoint).getPointLength().intValue();
						int distanceNextLength = 0;
						//判断是否终点,不是终点才计算
						if(iPoint != routePointList.size()-1) {
							distanceNextLength = routePointList.get(iPoint+1).getSectionLength().intValue();
							speed = routePointList.get(iPoint+1).getSpeed();
						} else {
							speed = routePointList.get(iPoint).getSpeed();
						}
						objectMap.put("iPoint", iPoint);
						objectMap.put("x", pointX);
						objectMap.put("y", pointY);
						objectMap.put("distanceStartLength", distanceStartLength);//距起点距离
						objectMap.put("distanceNextLength", distanceNextLength);//距下个途径点距离
						objectMap.put("speed", speed);
						objectMap.put("videoFlag", videoFlag);
						objectMap.put("videoMap", videoMapTemp);
						objectMap.put("signalFlag", signalFlag);
						objectMap.put("signalMap", signalMapTemp);
						objectMap.put("taskName", taskName);
						//如果是实际执行线路时：不考虑途径点的到达时间，某途径点离开时间为当前时间时刻加上停留时间
						if(isExecute) {
							isOverTime = false;
							leaveTimeL = timeKey + nextParkTimeL;
						}
						//时间戳小于离开时间时，该时间段内都停留在途径点位置
						for(long time=timeKey; timeKey<leaveTimeL; timeKey+=routeSplitTime) {
							//取出timeMap中原有的值，如果存在则更新，不存在则新增
							Map<String, Map> taskMapTemp = timeMap.get(String.valueOf(timeKey));
							if(taskMapTemp == null) {
								taskMap.put(taskId, objectMap);
								timeMap.put(String.valueOf(timeKey), taskMap);
							}else {
								taskMapTemp.put(taskId, objectMap);
								timeMap.put(String.valueOf(timeKey), taskMapTemp);
							}
							/*String dateStr = TimeHelper.dateToLongString(new Date(timeKey));
							String message =  dateStr+ "---------执行任务：" + taskId + "===下一个时间："+pointX+"--距起点："+pointY;
							logger.info(message);*/
						}
					}else {
						double[] geo = geoToolsHelper.LinearEquationXY(routeInfo.getRouteGeo(), distanceStartLength); // 取得经纬度
						int distanceNextLength = routePointList.get(iPoint+1).getPointLength().intValue() - (int)distanceStartLength;
						if(geo == null) {
							//默认终点坐标
							double endX = endRoutePoint.getPointX();
							double endY = endRoutePoint.getPointY();
							objectMap.put("x", endX);
							objectMap.put("y", endY);
							xxx = endX;
							yyy = endY;
						}else {
							objectMap.put("x", geo[0]);
							objectMap.put("y", geo[1]);
							xxx = geo[0];
							yyy = geo[1];
						}
						objectMap.put("iPoint", iPoint);
						objectMap.put("distanceStartLength", distanceStartLength);//距起点距离
						objectMap.put("distanceNextLength", distanceNextLength);//距下个途径点距离
						objectMap.put("speed", speed);
						objectMap.put("videoFlag", videoFlag);
						objectMap.put("videoMap", videoMapTemp);
						objectMap.put("signalFlag", signalFlag);
						objectMap.put("signalMap", signalMapTemp);
						objectMap.put("taskName", taskName);
					}
					//取出timeMap中原有的值，如果存在则更新，不存在则新增
					Map<String, Map> taskMapTemp = timeMap.get(String.valueOf(timeKey));
					if(taskMapTemp == null) {
						taskMap.put(taskId, objectMap);
						timeMap.put(String.valueOf(timeKey), taskMap);
					}else {
						taskMapTemp.put(taskId, objectMap);
						timeMap.put(String.valueOf(timeKey), taskMapTemp);
					}
					//更新下一间隔的时间、距离
					timeKey += routeSplitTime;//下一间隔时间
					double m = (speed * 1000 / 3600)*routeSplitTime/1000;//每间隔距离
					distanceStartLength += m;//下一间隔距离
					//关联设备的是否推送标记重置为false
					videoFlag = false;
					signalFlag = false;
					/*String dateStr = TimeHelper.dateToLongString(new Date(timeKey));
					String message =  dateStr+ "---------执行任务：" + taskId + "===下一个时间："+xxx+"--距起点："+yyy;
					logger.info(message);*/ 
				}
				long time3 = new Date().getTime();
				//System.out.println(taskName+" 线路拆分循环时间"+ (time3 - time2) +"ms--获取拆分基础数据时间"+(time2 - time1)+"ms");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//long end = new Date().getTime();
		//System.out.println("线路拆分结束时间"+ (end - start) +"ms");
		return timeMap;
	}
}
